from django.shortcuts import render_to_response
from django.views.decorators.csrf import requires_csrf_token
from django.http import HttpResponse
from rest_framework_jwt.utils import jwt_decode_handler
import datetime
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect
from appapi.models import useradd
from django.template import RequestContext
import logging
from django.contrib.auth.decorators import login_required
logger = logging.getLogger(__name__)


@requires_csrf_token
@login_required
def index(request):
    logger.error(u'提示信息')
    logger.error('request.COOKIES:')
    logger.error(request.COOKIES)
    token  = request.COOKIES.get('csrftoken')
    logger.error(u'token:'+token)
    #user_info = jwt_decode_handler(token)
    #logger.error(user_info)
    return render_to_response('index.html')
'''    current_datetime
'''

def register(request):
    #token  = request.COOKIES.get('csrftoken')
    #logger.info(u'token:'+token)
    if request.method == 'POST':
        user_name = request.POST.get("username")
        address = request.POST.get("address")
        password1 = request.POST.get("password1")
        password2 = request.POST.get("password2")
        logger.error(u'token121212:' )
        user = User()
        user.username = user_name
        if user.validate_unique():
            logger.error(u'token11:')
        else:
            logger.error(u'token22:' )

        user.set_password(password1)
        user.email = 'aa@163.com'
        user.save()
        useradd1 = useradd()
        useradd1.address = address
        useradd1.user = user
        useradd1.save()
        return HttpResponseRedirect("/api-auth/login/?next=/")
    else:
        return render_to_response('register.html')